package com.keldian.mobilesafe.recevier;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.keldian.mobilesafe.activity.ClearData;
import com.keldian.mobilesafe.activity.LockScreen;
import com.keldian.mobilesafe.service.GPSService;
import com.keldian.mobilesafe.utils.SharePrefenceTool;
import com.keldian.mobilesafe.R;

/**
 * Created by Administrator on 15-12-17.
 */
public class SMSReceiver extends BroadcastReceiver {


    private static final String TAG = "SMSReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        // 写接收短信的代码
        Object[] objs = (Object[]) intent.getExtras().get("pdus");


        for (Object b : objs) {
            //具体的某一条短信
            SmsMessage sms = SmsMessage.createFromPdu((byte[]) b);
            //发送者
            String sender = sms.getOriginatingAddress();//15555555556
            String safenumber = SharePrefenceTool.getSafeMobile(context);//5556
            //5556
            ///1559999995556
            Log.i(TAG, "====sender==" + sender);
            String body = sms.getMessageBody();

            if (sender.contains(safenumber)) {

                if ("#*location*#".equals(body)) {
                    //得到手机的GPS
                    Log.i(TAG, "得到手机的GPS");
                    Toast.makeText(context, "GPS", Toast.LENGTH_SHORT).show();


//                    //启动服务
                    Intent i = new Intent(context, GPSService.class);
                    context.startService(i);
                    String lastlocation = SharePrefenceTool.getGPS(context);
                    Toast.makeText(context, "GPS", Toast.LENGTH_SHORT).show();
                    if (TextUtils.isEmpty(lastlocation)) {
                        //位置没有得到
                        SmsManager.getDefault().sendTextMessage(sender, null, "geting loaction.....", null, null);
                    } else {
                        SmsManager.getDefault().sendTextMessage(sender, null, lastlocation, null, null);
//                        把这个广播终止掉
                    }
                    abortBroadcast();
                } else if ("#*alarm*#".equals(body)) {
                    //播放报警影音
                    Log.i(TAG, "播放报警影音");
                    Toast.makeText(context, "Warning", Toast.LENGTH_SHORT).show();
                    MediaPlayer player = MediaPlayer.create(context, R.raw.ylzs);
                    player.setLooping(false);//
                    player.setVolume(1.0f, 1.0f);
                    player.start();

                    abortBroadcast();
                } else if ("#*wipedata*#".equals(body)) {
                    //远程清除数据
                    Intent intent3 = new Intent(context, ClearData.class);
                    intent3.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent3);
                    abortBroadcast();
                    Log.i(TAG, "远程清除数据");
                } else if ("#*lockscreen*#".equals(body)) {
                    //远程锁屏
                    Intent intent2 = new Intent(context, LockScreen.class);
                    intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent2);
                    abortBroadcast();
                }
            }
        }


    }


}


